<?php
namespace  Api\Controller;

class FundController{
    /**
     * 提现
     * @param double money
     * @param int bankcard_id
     */
    public function withdraw(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        params_exist(array('money','bankcard_id'));
         
        $money=I('post.money');
        $bankcard_id=I('post.bankcard_id');
        !M('bank_card')->where(array('uid'=>API_UID,'id'=>$bankcard_id))->find() && response(-2,'','银行卡不存在');
         
        $map=array();
        $map['uid']=API_UID;
    
        $M_ember=M('member');
         
        $userInfo=$M_ember->where($map)->find();
         
        ! preg_match('/^[0-9]+(.[0-9]{2})?$/', $money) && response(- 1, '提现金额格式错误');
         
        $m_cash=M('Cash');
    
        if((double)$userInfo['amount']<$money){
            response(-1,'','提现金额不能大于余额');
        }
         
        $map=array('bank_card_id'=>$bankcard_id,'money'=>$money,'status'=>0,
            'applytime'=>NOW_TIME,'uid'=>API_UID);
         
        $result=$m_cash->add($map);
         
        if($result){
            $bill_data = array();
            $bill_data['uid'] = API_UID;
            $bill_data['create_time'] = NOW_TIME;
            $bill_data['money'] = $money;
            $bill_data['sign'] = '-';
            $bill_data['type'] = 2;
            $bill_data['status'] = 1;
            $bill_data['title'] = '提现' ;
            $bill_data['desc'] = '提 现 金额:' . $money;
            M('bill')->add($bill_data);
             
            $sub=$M_ember->where(array('uid'=>API_UID))->setInc('amount',-$money);
            response(1,'','提现成功，请等待商家打款');
        }
         
    }
    
    /**
     * 我的账单
     * @param int starttime
     * @param int endtime
     * @param int page
     * @param int pagesize
     */
    public function billList(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        
        $starttime=I('post.starttime');
        $endtime=I('post.endtime');
        $page=I('post.page');
        (int) $page <=0 && $page=1;
        $pagesize=I('post.pagesize');
        (int) $pagesize <=0 && $pagesize=10;
        $offset=($page-1)*$pagesize;
        
        $M_member=M('member');
        $M_bill=M('bill');
        
        $map=array();
        $map['uid']=API_UID;
        
        if($starttime && $endtime){
            $starttime = I('time_start', date('Y-m-d', NOW_TIME));
            $endtime = I('time_end', date('Y-m-d', NOW_TIME));
            
            $begintime = strtotime($starttime . " 00:00:00");
            $endtime = strtotime($endtime . " 23:59:59");
            //$map['create_time']=array('bettwen')
            
            $map['create_time']=array('between',array($begintime,$endtime));
        }
        
        $list=$M_bill->where($map)->order('id DESC')->limit($offset,$pagesize)
        ->field('id,create_time time,title,desc,money,sign,type')
        ->select();
         $total=$M_bill->where($map)->count();
         foreach ($list as &$item){
             $item['format_time']=time_format($item['time'],'Y-m-d H:m:s');
         }
         int_to_string($list,array('type'=>array(1=>'充值', 2=>'消费', 3=>'冻结', 4=>'解冻')));
         $result=array();
         $result['total']=$total;
         $result['result']=$list;
         $list ? response(1,$result) : response(-2);         
        
    }
}
?>